//
//  PushForPush.m
//  HYHoney
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PushForPush.h"
#import "ModelForPush.h"
#import "UIImageView+WebCache.h"
#import "DKNightVersion.h"
#import "DataBaseHandle.h"

@interface PushForPush ()<UIWebViewDelegate>

@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UITableView *tableV;

@property (nonatomic, retain)UILabel *labelForTitle;
@property (nonatomic, retain)UIWebView *web;

@property (nonatomic, retain)ModelForPush *model;
@property (nonatomic, retain)UIButton *button;
@property (nonatomic, assign)BOOL isSave;

@end

@implementation PushForPush

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x27408B);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init];
    
    self.navigationItem.rightBarButtonItem.customView = [[UIView alloc] init];
    
    self.navigationItem.rightBarButtonItem.customView.frame = CGRectMake(40, 40, 25, 25);
    
    [self create];
    [self createButton];
}



- (void)dealloc{

    [_labelForTitle release];
    [_arr release];
    [_web release];
    [_tableV release];
    [super dealloc];
}


- (void)create{
    
    self.labelForTitle = [[UILabel alloc] init];
    [self.view addSubview:self.labelForTitle];
}


-(void) createButton {
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, self.navigationItem.rightBarButtonItem.customView.frame.size.width, self.navigationItem.rightBarButtonItem.customView.frame.size.height);
    [self.button setBackgroundImage:[UIImage imageNamed:@"nolike"] forState:UIControlStateNormal];
    
    [self.navigationItem.rightBarButtonItem.customView addSubview:self.button];
    
    [self.button addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)addAction:(UIButton *)button {

    if (self.isSave == NO) {
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
        NSLog(@"%@", self.model.title);
        [[DataBaseHandle shareDataBaseHandle] insertStudent:self.model];
        
        
        self.isSave = YES;
    }
    
    else {
        
        [[DataBaseHandle shareDataBaseHandle] deleteWithModel:self.model];

        
        [self.button setBackgroundImage:[UIImage imageNamed:@"nolike"] forState:UIControlStateNormal];
        
        self.isSave = NO;
        
    }
}


- (void)click:(UIBarButtonItem *)sender{
    
}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.arr = [NSMutableArray array];
    self.arr = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
    
    for (ModelForPush *model in self.arr) {
        if ([model.title isEqualToString:self.model.title]) {
            
            [self.button setBackgroundImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
            self.isSave = YES;
            
            break;
            
        }
        else {
            
            [self.button setBackgroundImage:[UIImage imageNamed:@"nolike"] forState:UIControlStateNormal];
            
            self.isSave = NO;
        }
    }
    
}






- (void)passModelForPush:(ModelForPush *)model{
    
    self.model = model;
    
    CGRect frame = self.view.frame;
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 10 + 70, CGRectGetWidth(frame), CGRectGetHeight(frame) - 70)];
    self.web.userInteractionEnabled = YES;
    self.web.delegate = self;
    self.web.opaque = NO;
    
    NSURL *url = [NSURL URLWithString:model.content_url];
    [self.web loadRequest:[NSURLRequest requestWithURL:url]];
    
    self.web.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
    [self.view addSubview:self.web];
    
    self.labelForTitle.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, 70);
    self.labelForTitle.text = model.title;
    self.labelForTitle.font = [UIFont boldSystemFontOfSize:20];
    self.labelForTitle.numberOfLines = 0;
    self.labelForTitle.dk_textColorPicker = DKColorWithRGB(0x000000, 0xF0FFFF);
}




- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
        [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#27408B'"];

        [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='#87CEFF'"];
        
    }else {
        [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#ffffff'"];
        
        [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='#000000'"];
    }
    
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
//        [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#27408B'"];
//        
//    }else {
//        [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#ffffff'"];
//    }

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
